const {
  MarketingModel
} = require('../../../../utils/marketing-model.js');
var marketingModel = new MarketingModel();
var wxcode = require('../../../../utils/index.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activityVerificationId: "", //核销记录id
    ficationInfo: {}, //核销信息
    timer: '', //定时器名字
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    this.setData({
      activityVerificationId:options.activityVerificationId,
    }, function() {
      this.getVerificationGet();
      this.createQrCode();//创建二维码
    })
  },

  /**
   * 奖品核销详情
   */
  getVerificationGet: function() {
    clearInterval(this.data.timer);
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId'); //商户ID
    param.activityVerificationId = this.data.activityVerificationId; //核销记录id
    param.activityCardType = 1; //活动类型：1：集卡活动一期 2：全国活动 3：达人机活动
    marketingModel.verificationGet(param).then(data => {
      this.setData({
        ficationInfo: data,    
      },function(){
        this.startReportHeart();
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 设置二维码
   */
  createQrCode: function() {
    var that = this;
    wxcode.qrcode(that.data.activityVerificationId, "myQrcode", 360, 360)
  },

  /**
   * 计时器
   */
  startReportHeart: function () {
    var that = this;
    that.setData({
      timer: setInterval(function () { //这里把setInterval赋值给变量名为timer的变量    
        that.getVerificationGet();//奖品核销详情
      }, 5000)
    })
  },

  /**
   * 返回首页
   */
  goHome: function() {
    wx.reLaunch({
      url: '/pages/memberCenter/index/index'
    })
  },
  /**
 * 生命周期函数--监听页面隐藏
 */
  onHide: function () {
    clearInterval(this.data.timer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
  },
})